<template>

</template>

<script>
import request from "../utils/request";

export default {
    onLoad: function () {
        uni.login({
            success(res) {
                if (res.code) {
                    request.get("/store/get/OpenIdByWxCode/" + res.code).then(res => {
                        console.log("用户OpenId "+res.data)
                        uni.setStorageSync(
                            'openId',
                             res.data
                        )
                        if (uni.getStorageSync('userMain')!==""){
                            if (res.data!==(uni.getStorageSync('userMain').openId)){
                                request.get("/store/get/userMainByOpenId/"+res.data).then(res=>{
                                    console.log("用户信息 "+res.data)
                                    if(res.data!=null){
                                        uni.setStorageSync('userMain', res.data)
                                        uni.reLaunch({
                                            url:"/pages/home/index"
                                        })
                                    }else {
                                        console.log("OpenId未绑定")
                                        uni.reLaunch({
                                            url:"/pages/login/index"
                                        })
                                        uni.showToast({
                                            title:"微信未绑定",
                                            icon:"error",
                                            duration: 2000,
                                        })

                                    }
                                })
                            }else {
                                uni.reLaunch({
                                    url:"/pages/home/index"
                                })
                            }
                        }else {
                            request.get("/store/get/userMainByOpenId/"+res.data).then(res=>{
                                console.log("用户信息 "+res.data)
                                if(res.data!=null){
                                    uni.setStorageSync('userMain', res.data)
                                    uni.reLaunch({
                                        url:"/pages/home/index"
                                    })
                                }else {
                                    console.log("OpenId未绑定")
                                    uni.reLaunch({
                                        url:"/pages/login/index"
                                    })
                                    uni.showToast({
                                        title:"微信未绑定",
                                        icon:"error",
                                        duration: 2000,
                                    })
                                }
                            })
                        }
                    })
                } else {
                    uni.showToast({
                        title:"微信未登录",
                        icon:"error",
                        duration: 2000,
                    })
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })

    }
}
</script>

<style scoped>

</style>